package org.paradise.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.paradise.dao.AdminDao;
import org.paradise.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminDao adminDao;

    //查询所有用户数据调用的方法
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, Admin condition) throws Exception{
        Map<String,Object> map = new HashMap<>();
        Page<Admin> pager = PageHelper.startPage(page,limit);
        List<Admin> list = adminDao.getList(condition);
        map.put("count",pager.getTotal());  //条数
        map.put("data",list);   //数据
        map.put("code",0);   //状态
        map.put("msg","");   //返回的日志
        return map;
    }

    //添加管理员
    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> add(Admin admin)throws Exception{
        Map<String,Object> map = new HashMap<>();
        if (admin.getState() == null){
            admin.setState(0);
        }
        adminDao.add(admin);
        map.put("result",true);
        return map;
    }
}
